<!--title-->
<div class="mnav1">
	<div class="m_nav">
		<div class="nav_left">
			<em class="home_icon"><?=$this->sys_name?></em><span>&nbsp;&gt;&nbsp;</span><font><?=$this->title?></font>
		</div>
	</div>
</div>

<div class="content-table">
<div class="content-table-title"><?=$this->title?></div>
<br />
<?php if(!empty($_SESSION['contract']['client_info'])):?>
<div class="replace" id="contract-in-session">您有未完成的合同 <a href="/contracts/add-contract-step2">【继续完成合同】</a> 或 <a href="" onclick="CancelContract()">【放弃合同】</a></div>
<br id="contract-in-session-br" />
<?php endif;?>
<?php if($this->msg):?>
<div class="replace"><?=$this->msg?></div>
<br />
<?php endif;?>
<form action="/contracts/index" name="form1" method="GET">
合同类型：<input type="radio" name="contract_type_id" id="contract_type_id_0" value="0" <?php if(!$this->contract_type_id):?>checked="checked"<?php endif;?> /><label for="contract_type_id_0"> 所有</label>&nbsp;&nbsp;&nbsp;<input type="radio" name="contract_type_id" id="contract_type_id_1" value="1" <?php if(1==$this->contract_type_id):?>checked="checked"<?php endif;?> /><label for="contract_type_id_1"> 付费合同</label>&nbsp;&nbsp;&nbsp;<input type="radio" name="contract_type_id" id="contract_type_id_2" value="2" <?php if(2==$this->contract_type_id):?>checked="checked"<?php endif;?> /><label for="contract_type_id_2"> 免费合同</label>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
支付方式：<input type="radio" name="pay_type" id="pay_type_0" value="0" <?php if(!$this->pay_type):?>checked="checked"<?php endif;?> /><label for="pay_type_0"> 所有</label>&nbsp;&nbsp;&nbsp;<input type="radio" name="pay_type" id="pay_type_1" value="1" <?php if(1==$this->pay_type):?>checked="checked"<?php endif;?> /><label for="pay_type_1"> 预付</label>&nbsp;&nbsp;&nbsp;<input type="radio" name="pay_type" id="pay_type_2" value="2" <?php if(2==$this->pay_type):?>checked="checked"<?php endif;?> /><label for="pay_type_2"> 后付</label>&nbsp;&nbsp;&nbsp;<input type="radio" name="pay_type" id="pay_type_3" value="3" <?php if(3==$this->pay_type):?>checked="checked"<?php endif;?> /><label for="pay_type_3"> 分期</label>&nbsp;&nbsp;&nbsp;<input type="radio" name="pay_type" id="pay_type_4" value="4" <?php if(4==$this->pay_type):?>checked="checked"<?php endif;?> /><label for="pay_type_4"> 免费</label>
<br /><br />
<?php if("LEVEL-1" != $_SESSION['identity']['level']):?>
销售：<select name="sales_emp_id">
		<option value="">不限</option>
		<?php if(!empty($this->sales)):?>
		<?php foreach($this->sales as $sales_emp_id => $sales_name):?>
		<?php if($sales_name):?>
		<option value="<?=$sales_emp_id?>"<?php if($sales_emp_id==$this->sales_emp_id):?> selected="selected"<?php endif;?>><?=$sales_name?></option>
		<?php endif;?>
		<?php endforeach;?>
		<?php endif;?>
	</select>
&nbsp;&nbsp;&nbsp;&nbsp;
<?php endif;?>
产品：<select name="product_id">
		<option value="">不限</option>
		<?php if(!empty($this->products)):?>
		<?php foreach($this->products as $product_id => $product_name):?>
		<option value="<?=$product_id?>"<?php if($product_id==$this->product_id):?> selected="selected"<?php endif;?>><?=$product_name?></option>
		<?php endforeach;?>
		<?php endif;?>
	</select>
&nbsp;&nbsp;&nbsp;&nbsp;
乙方单位：<select name="owner_id">
		<option value="">不限</option>
		<?php if(!empty($this->owners)):?>
		<?php foreach($this->owners as $owner_id => $owner_name):?>
		<option value="<?=$owner_id?>"<?php if($owner_id==$this->owner_id):?> selected="selected"<?php endif;?>><?=$owner_name?></option>
		<?php endforeach;?>
		<?php endif;?>
	</select>
&nbsp;&nbsp;&nbsp;&nbsp;
审批状态：<select name="status">
		<option value="">不限</option>
		<?php foreach($this->statuses as $status_id => $status_name):?>
		<option value="<?=$status_id?>" <?php if(is_numeric($this->status) && $status_id==$this->status){echo "selected='selected'";}?>><?=$status_name?></option>
		<?php endforeach;?>
	</select>
<br /><br />
省：<select name="provinceId" id="provinceId" onchange="GetCities(this.options[this.options.selectedIndex].value);">
	<option value="">不限</option>
	<?php if(!empty($this->provinces)):?>
	<?php foreach($this->provinces as $provinceId => $provinceName):?>
	<option value="<?=$provinceId?>" <?php if($provinceId==$this->provinceId){echo "selected='selected'";}?>><?=$provinceName?></option>
	<?php endforeach;?>
	<?php endif;?>
</select>
&nbsp;&nbsp;
市：<select name="cityId" id="cityId">
		<option value="">不限</option>
		<?php if(!empty($this->init_cities)):?>
		<?php foreach($this->init_cities as $init_cities):?>
		<option value="<?=$init_cities['cityId']?>" <?php if($init_cities['cityId']==$this->cityId){echo "selected='selected'";}?>><?=$init_cities['cityName']?></option>
		<?php endforeach;?>
		<?php endif;?>
	</select>
&nbsp;&nbsp;&nbsp;&nbsp;
合同起期：<input type="text" name="start_date_start" id="start_date_start" value="<?=$this->start_date_start?>" style="width: 100px" autocomplete="off"> ~ <input type="text" name="start_date_end" id="start_date_end" value="<?=$this->start_date_end?>" style="width: 100px" autocomplete="off">&nbsp;&nbsp;&nbsp;&nbsp;
合同止期：<input type="text" name="expired_date_start" id="expired_date_start" value="<?=$this->expired_date_start?>" style="width: 100px" autocomplete="off"> ~ <input type="text" name="expired_date_end" id="expired_date_end" value="<?=$this->expired_date_end?>" style="width: 100px" autocomplete="off">
<br />
关键字：<input type="text" name="keyword" <?php if($this->keyword){echo "value='".$this->keyword."'";}?> />
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" class="button" value="搜索" />
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" class="button-cancle" value="清空搜索条件" onclick="location.href='?';" />
&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;
<font color="red"><b>当前搜索结果: 合同数 <?=$this->data['ct']?></b></font>
</form>
<select id="export_type">
	<option value="">报表</option>
	<option value="<?=$this->export_link?>">订单报表</option>
	<option value="<?=$this->export_link2?>">发布明细需求</option>
</select>
&nbsp;
<button onclick="ExportReport()">导出</button>
<br />
<br />
<table>
  <tr>
	<th>执行</th>
    <th>合同类型</th>
    <th>合同编号</th>
    <th>合同开始</th>
    <th>合同结束</th>
    <th>客户</th>
    <th>合同金额</th>
    <th>应收</th>
    <th>已收</th>
    <th>销售</th>
    <th>备注</th>
    <th>开票</th>
    <th>审批状态</th>
    <th>操作</th>
  </tr>
  <?php $data = $this->data['current_page_info'];?>
  <?php if($data["client_name"]):?>
  <?php $nn=1;?>
  <?php foreach($data["client_name"] as $id => $something):?>
  <?php
  $no_show_invoice = 1;
  
  if($data["start_date"][$id])
  {
	  $current_start_time = mktime(0,0,0, substr($data["start_date"][$id], 5, 2), substr($data["start_date"][$id], 8, 2), substr($data["start_date"][$id], 0, 4));
	  $sys_first_time = mktime(0,0,0,4,1,2019);
	  
	  if($current_start_time >=  $sys_first_time)
	  {
		  $no_show_invoice = 0;
	  }
  }
  ?>
  <tr>
	<td><?php if(1==$data["launch_switcher"][$id]){echo "<a title='待确认执行' style='font-size:20px;color:#FFD700;text-decoration:none;'>●</a>";}elseif(2==$data["launch_switcher"][$id]){echo "<a title='已执行' style='font-size:20px;color:#228B22;text-decoration:none;'>●</a>";}else{echo "<a title='未执行' style='font-size:20px;color:#FF4500;text-decoration:none;'>●</a>";}?></td>
    <td><?=$data["contract_type_name"][$id]?></td>
    <td><?=$data["contract_sid"][$id]?></td>
    <td><?=$data["start_date"][$id]?></td>
    <td><?=$data["expired_date"][$id]?></td>
	<td><?=$data["client_name"][$id]?></td>
    <td><?=number_format($data["contract_value"][$id], 0, ".", ",")?></td>
    <td><?=number_format($data["receivable_value"][$id], 0, ".", ",")?></td>
    <td><?=number_format($data["received_value"][$id], 0, ".", ",")?></td>
	<td><?=$data["sales_emp_name"][$id]?></td>
	<td><font color='red'><?=$data["tips"][$id]?"<a title='".$data["tips"][$id]."'>☢</a>":""?></font></td>
	<td>
		<?php
		if(3 == $data["invoice_done"][$id])
		{
			echo "<span title='已开票' style='color:green;'>已开票</span>";
		}elseif(2 == $data["invoice_done"][$id])
		{
			echo "<span title='开票中' style='color:red;'>开票中</span>";
		}elseif(1 == $data["invoice_done"][$id])
		{
			echo "<input type='button' value='继续申请' onclick=\"window.location.href='/contracts/invoice-apply/contract_id/".$id."'\" />";
		}elseif(!$data["invoice_done"][$id] && $data["contract_sid"][$id] && $data["sales_emp_id"][$id] == $_SESSION['identity']['userid'] && !$no_show_invoice && '付费合同' == $data["contract_type_name"][$id])
		{
			echo "<input type='button' value='申请' onclick=\"window.location.href='/contracts/invoice-apply/contract_id/".$id."'\" />";
		}
		?>
	</td>
	<td><?php if($data["allow_edit"][$id]):?>驳回修改<?=isset($data["reject_emp"][$id])?"(".$data["reject_emp"][$id].")":""?><br><font color='red'><?=isset($data["reject_reason"][$id])?$data["reject_reason"][$id]:""?></font><?php else:?><?=$this->statuses[$data["status"][$id]]?><?php if(!$data["status"][$id]){echo "(".$data["next_leader_name"][$id].")";}?><?php endif;?></td>
    <td>
		<button onclick="window.open('/contracts/view-contract/contract_id/<?=$id?>')">查看</button>
		<?php if(2 == $data['product_id'][$id] && 1099 == $data['sku_id'][$id]):?>
		&nbsp;&nbsp;<button onclick="window.location.href='/contracts/add-base-contract/contract_id/<?=$id?>'">充值</button>
		<?php endif;?>
		<?php if(7 != $data["status"][$id]):?>
		<?php if("LEVEL-1" == $_SESSION['identity']['level']):?>
			<?php if($data["allow_edit"][$id]):?>
			&nbsp;&nbsp;<button onclick="window.open('/contracts/edit-contract-type2/contract_id/<?=$id?>')">编辑</button>
			<?php endif;?>
			<?php if(0 == $data["status"][$id]):?>
			&nbsp;&nbsp;<button onclick="ContractOpt(<?=$id?>, 7)">作废</button>
			<?php endif;?>
		<?php elseif("LEVEL-2" == $_SESSION['identity']['level']):?>
			<?php if(isset($data["next_leader_id"][$id])):?>
			<?php if($data["next_leader_id"][$id] == $_SESSION['identity']['userid'] && 0 == $data["status"][$id] && !$data["allow_edit"][$id]):?>
			&nbsp;&nbsp;<button onclick="ContractOpt(<?=$id?>, 1)">通过</button>&nbsp;&nbsp;<button onclick="ContractOpt(<?=$id?>, 0)">驳回</button>
			<?php endif;?>
			<?php endif;?>
			<?php if($data["allow_edit"][$id]):?>
			&nbsp;&nbsp;<button onclick="window.open('/contracts/edit-contract-type2/contract_id/<?=$id?>')">编辑</button>
			<?php endif;?>
			<?php if($_SESSION['identity']['userid'] == $data["sales_emp_id"][$id] && in_array($data["status"][$id],array(0,5))):?>
			&nbsp;&nbsp;<button onclick="ContractOpt(<?=$id?>, 7)">作废</button>
			<?php endif;?>
		<?php elseif("LEVEL-4" == $_SESSION['identity']['level']):?>
			&nbsp;&nbsp;<button onclick="window.open('/contracts/edit-contract-type2/contract_id/<?=$id?>')">修改合同</button>
			&nbsp;&nbsp;<button onclick="window.open('/contracts/view-contract/contract_id/<?=$id?>')">查看</button>
		<?php endif;?>
		<?php endif;?>
	</td>
  </tr>
  <?php endforeach;?>
  <?php endif;?>
</table>
</div>
<br /><br />
<div align="right"><?=$this->pagination?></div>
<br /><br /><br />

<script>
$(function() {
	$( "#start_date_start" ).datepicker({ dateFormat: 'yy-mm-dd'});
});
$(function() {
	$( "#start_date_end" ).datepicker({ dateFormat: 'yy-mm-dd'});
});
$(function() {
	$( "#expired_date_start" ).datepicker({ dateFormat: 'yy-mm-dd'});
});
$(function() {
	$( "#expired_date_end" ).datepicker({ dateFormat: 'yy-mm-dd'});
});

function GetCities(provinceId)
{
	$.ajax({
			type: "post",
			url : "/ajax/get-cities",
			dataType:'json',
            async:false,
			data: {
					'provinceId':provinceId
				  },
				success: function(json){
					if(json.result == 1 && typeof(json.data) != "undefined") {
                        var arr = json.data;
						var txt = "<option value=''>不限</option>";
						
                        for(i=0; i < arr.length; i++){
							txt += "<option value='" + arr[i]['cityId'] + "'>" + arr[i]['cityName'] + "</option>";
                        }
						
						document.getElementById("cityId").innerHTML = txt;
                    }
			}
		});
}

function ContractOpt(id, status)
{
	var msg = ""; 
	
	if(status == 1)
	{
		msg = "确定通过合同吗？";
	}else if(status == 2)
	{
		msg = "确定通过合同吗？";
	}else if(status == 0)
	{
		msg = "确定驳回合同吗？";
	}else if(status == 7)
	{
		msg = "确定作废合同吗？";
	}
	
	if (confirm(msg)==true){
		if(status != 0)
		{
			$.ajax({
				type: "post",
				url : "/ajax/contract-opt",
				dataType:'json',
				async:false,
				data: {
						'id':id,
						'status':status
					  },
					success: function(json){
						if(json.result == 1) {
							window.location.href = "/contracts/index/msg/2";
						}else{
							window.location.href = "/contracts/index/msg/3";
						}
				}
			});
		}else{
			window.open("/contracts/rejection/contract_id/" + id);
		}
	}else{ 
		return false; 
	}
}

function CancelContract()
{
	var msg = "如果放弃合同，您之前未完成的合同将会被清空，您可以重新申请合同，是否继续？"; 
	
	if (confirm(msg)==true){
		$.ajax({
			type: "post",
			url : "/ajax/cancel-contract-in-session",
			dataType:'json',
            async:false,
			data: {},
				success: function(json){
					$("#contract-in-session").hide();
					$("#contract-in-session-br").hide();
			}
		});
	}else{ 
		return false; 
	}
}

function ExportReport()
{
	var export_type = document.getElementById("export_type").value;
	
	if(export_type.length > 0)
	{
//		window.location.href = export_type;
window.location.href = export_type;
	}
}
</script>